package com.qdu.impl;

import com.qdu.dao.ShoppingCar;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author SBH
 */
public class ShoppingCarImpl implements ShoppingCar {

    private int SUCCESS = 1;
    private int ERROR = 2;
    private int SAME = 3;

    @Override
    public int addGouWuChe(int shoppingcar_bookid, String shoppingcar_userid) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        Random random = new Random();

        int r = random.nextInt(100);

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select shoppingcar_amount from shoppingcar where shoppingcar_bookid=? and shoppingcar_userid=?");
            ps.setInt(1, shoppingcar_bookid);
            ps.setString(2, shoppingcar_userid);
            rs = ps.executeQuery();
            con.commit();
            if (rs.next()) {
                int j = rs.getInt(1);
                j += 1;
                // 修改购物车
                ps = con.prepareStatement("update shoppingcar set shoppingcar_amount=? where shoppingcar_bookid=?");
                ps.setInt(1, j);
                ps.setInt(2, shoppingcar_bookid);
                int x = ps.executeUpdate();
                if (x == 1) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } else {
                // 向2购物车表中添加信息
                ps = con.prepareStatement("insert into shoppingcar(shoppingcar_id,shoppingcar_bookid,shoppingcar_amount,shoppingcar_addtime,shoppingcar_userid) values(?,?,?,?,?)");
                ps.setInt(1, r);
                ps.setInt(2, shoppingcar_bookid);
                ps.setInt(3, 1);
                ps.setString(4, new java.util.Date().toLocaleString());
                ps.setString(5, shoppingcar_userid);
                int i = ps.executeUpdate();
                if (i == 1) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

    @Override
    public int addOrder(long number, String name, String fukuanfangshi) {
        Connection con = null;
        PreparedStatement ps = null;
        List list = new ArrayList();
        ResultSet rs = null;
        ResultSet rs2 = null;
        Random random = new Random();

        int r = random.nextInt(10000);

        int a = random.nextInt(10000);

        try {
            con = DatabaseUtil.getConnection();

            ps = con.prepareStatement("insert into ordersendingform(ordersendingform_id,ordersendingform_formnumber,ordersendingform_userid,ordersendingform_sendok,fangshi,zhuangtai) values(?,?,?,?,?,?)");
            ps.setInt(1, r);
            ps.setLong(2, number);
            ps.setString(3, name);
            ps.setString(4, "未发货");
            ps.setString(5, fukuanfangshi);
            ps.setString(6, "正常");
            int m = ps.executeUpdate();
            con.commit();
            if (m == 1) {
                ps = con.prepareStatement("select shoppingcar_bookid,shoppingcar_amount from shoppingcar where shoppingcar_userid=?");
                ps.setString(1, name);
                rs2 = ps.executeQuery();
                while (rs2.next()) {
                    int bookid = rs2.getInt(1);
                    int shuliang = rs2.getInt(2);
                    ps = con.prepareStatement("insert into bookorderform(bookorderform_id,bookorderform_formnumber,bookorderform_bookid,bookorderform_amount) values(?,?,?,?)");
                    ps.setInt(1, a);
                    ps.setLong(2, number);
                    ps.setInt(3, bookid);
                    ps.setInt(4, shuliang);
                    ps.executeUpdate();
                }
                ps = con.prepareStatement("delete shoppingcar where shoppingcar_userid=?");
                ps.setString(1, name);
                ps.executeUpdate();
                return this.SUCCESS;

            } else {
                return this.ERROR;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    @Override
    public int delDD(int ordersendingform_id) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("update ordersendingform set zhuangtai='用户已删除' where ordersendingform_id=?");
            ps.setInt(1, ordersendingform_id);
            con.commit();
            int n = ps.executeUpdate();
            if (n == 1) {
                return this.SUCCESS;
            } else {
                return this.ERROR;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    @Override
    public int upGouWuChe(int id, int shuliang, String name) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            ps = con.prepareStatement("update  shoppingcar set  shoppingcar_amount=? where shoppingcar_bookid=? and shoppingcar_userid=? ");
            ps.setInt(1, shuliang);
            ps.setInt(2, id);
            ps.setString(3, name);
            int c = ps.executeUpdate();
            if (c == 1) {
                return this.SUCCESS;
            } else {
                return this.ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }

    @Override
    public int delGouWu(int shoppingcar_bookid) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            ps = con.prepareStatement("delete from shoppingcar where shoppingcar_bookid=? ");
            ps.setInt(1, shoppingcar_bookid);

            int v = ps.executeUpdate();
            if (v == 1) {
                return this.SUCCESS;
            } else {
                return this.ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

    @Override
    public List getMyGouWuChe(String shoppingcar_userid) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            con = DatabaseUtil.getConnection();
            con.commit();
            ps = con.prepareStatement("select bookinformation.book_name,shoppingcar.shoppingcar_amount,bookinformation.book_price,shoppingcar.shoppingcar_addtime,shoppingcar.shoppingcar_bookid from bookinformation,shoppingcar where shoppingcar.shoppingcar_bookid=bookinformation.book_id and shoppingcar.shoppingcar_userid=? order by shoppingcar.shoppingcar_id desc ");
            ps.setString(1, shoppingcar_userid);
            rs = ps.executeQuery();
            while (rs.next()) {
                List list2 = new ArrayList();
                list2.add(rs.getString(1));
                list2.add(rs.getString(2));
                list2.add(rs.getString(3));
                list2.add(rs.getString(4));
                list2.add(rs.getString(5));
                list.add(list2);

            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }
}
